package com.surfilter.massdata.es.core.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.surfilter.massdata.es.core.client.EsClient;
import com.surfilter.massdata.es.core.client.pool.EsClientPool;
import com.surfilter.massdata.es.core.service.EsApiService;


public class EsApiServiceImpl implements EsApiService {
	
	
	private EsClientPool pool = null;

	public EsApiServiceImpl(EsClientPool pool) {
		super();
		this.pool = pool;
	}

    private static final Log LOG = LogFactory.getLog(EsApiServiceImpl.class);
    
	@Override
	public void deleteByIndex(String indexName) {
		EsClient esClient = pool.getEsClient();
		try {
			boolean exists = esClient.isExists(indexName);
			if(exists) {
				LOG.info("删除索引:" + indexName);
				esClient.deleteIndex(indexName);
				LOG.info("index deleted!!! indexName: " + indexName);
			}
		} catch (Exception e) {
			LOG.error("elasticsearch single delete error !" , e);
		} finally {
			if (esClient != null) {
				pool.removeEsClient(esClient);
			}
		}
	}
	/*@Override
	public void deleteByQuery(String indexName, String typeName,
			EsFieldEntity entity) throws IOException {

		if(null==entity){
			throw new IOException("entity is null error !");
		}
		EsClient esClient = pool.getEsClient();
		try {
			//判断索引是否存在
			boolean exists = esClient.isExists(indexName);
			if(exists){
				QueryBuilder query = 
						QueryBuilders.termsQuery(entity.getKey(), entity.getValue());
				//TODO 通过插件的方式删除数据
				DeleteByQueryResponse rsp = new DeleteByQueryRequestBuilder(
						esClient.getClient(), DeleteByQueryAction.INSTANCE)
	            .setTypes(typeName)
	            .setQuery(query)
	            .execute()
	            .actionGet();
				LOG.warn("--------------delete number:"+rsp.getTotalFound());
			}
		} catch (Exception e) {
			LOG.error("elasticsearch single delete error !" , e);
		} finally {
			if (esClient != null) {
				pool.removeEsClient(esClient);
			}
		}
	}
	@Override
	public void deleteByQuery(String indexName, String typeName,
			List<EsFieldEntity> list) throws IOException {
		if(null!=list && list.size()>0){
			BoolQueryBuilder source = QueryBuilders.boolQuery();
			for (EsFieldEntity entity : list) {
				source.must(QueryBuilders.termsQuery(entity.getKey(), entity.getValue()));
			}
			EsClient esClient = pool.getEsClient();
			try {
				//判断索引是否存在
				boolean exists = esClient.isExists(indexName);
				if(exists){
					//TODO 通过插件的方式删除数据
					DeleteByQueryResponse rsp = new DeleteByQueryRequestBuilder(
									esClient.getClient(), DeleteByQueryAction.INSTANCE)
				    .setIndices(indexName)
				    .setTypes(typeName)
				    .setQuery(source)
				    .execute()
				    .actionGet();
					LOG.warn("--------------delete by query number:"+rsp.getTotalFound());
				}
			} catch (Exception e) {
				LOG.error("elasticsearch single insert error !" , e);
			} finally {
				if (esClient != null) {
					pool.removeEsClient(esClient);
				}
			}
		}else{
			LOG.error("delete by query list is null !");
		}
	}
	@Override
	public void deleteByType(String indexName, String typeName)
			throws IOException {
		EsClient esClient = pool.getEsClient();
		try {
			//判断索引是否存在
			boolean exists = esClient.isExists(indexName);
			if(exists){
				QueryBuilder query = QueryBuilders.matchAllQuery();
				//TODO 通过插件的方式删除数据
				DeleteByQueryResponse rsp = new DeleteByQueryRequestBuilder(
						esClient.getClient(), DeleteByQueryAction.INSTANCE)
	            .setTypes(typeName)
	            .setQuery(query)
	            .execute()
	            .actionGet();
				LOG.warn("--------------delete type number:"+rsp.getTotalFound());
			}
		} catch (Exception e) {
			LOG.error("elasticsearch single delete error !" , e);
		} finally {
			if (esClient != null) {
				pool.removeEsClient(esClient);
			}
		}
	}*/
}
